<?php                                       
function createTemplateEngine()
{
    $smartyDirectory = SITE_ROOT . "/lib/smarty/";
    require_once($smartyDirectory . "Smarty.class.php");
    $engine = new Smarty();
    initTemplateEngine($engine);
    return $engine;
}

function initTemplateEngine($templateEngine)
{
    $templateEngine->template_dir = TEMPLATES_DIR;
    $templateEngine->compile_dir = TEMPLATES_C_DIR;
}
    
function assignTemplateArray($templateEngine, $array)
{
    foreach ($array as $key => $value)
    {
        $templateEngine->assign($key, $value);
    }
}

function getTemplate($template, $vars)
{
    $templateEngine = createTemplateEngine();
    assignTemplateArray($templateEngine, $vars);
    return $templateEngine->fetch($template);
}

function displayTemplate($template, $vars, $layout = "layout.tpl")
{
    $templateEngine = createTemplateEngine();
    $vars["content"] = getTemplate($template, $vars);
    assignTemplateArray($templateEngine, $vars);
    $templateEngine->display($layout);
}
?>